package com.edu.whty.controller;

import com.edu.whty.common.constants.CodeConstants;
import com.edu.whty.entity.base.ResponseEntity;
import com.edu.whty.entity.cloud.CatalogInfo;
import com.edu.whty.service.WxCloudService;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/wxCloud")
public class WxCloudController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
	private WxCloudService wxCloudService;

    /**
     *
     * @Desc 保存用户上次选择的教材章节
     * @param CatalogInfo  编目信息 实体类
     */
    @RequestMapping(value = "/saveCatalogUser", method = RequestMethod.POST)
    public ResponseEntity saveCatalogUser(HttpServletRequest request, HttpServletResponse response,
                                                @RequestBody CatalogInfo info) {
        try {
			if (null == info) {
				return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "请求参数不能为空");
			} else {
				if (StringUtils.isEmpty(info.getPeriodName())) {
					return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "periodName不能为空");
				}if (StringUtils.isEmpty(info.getSubjectName())) {
					return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "subjectName不能为空");
				}
				if (StringUtils.isEmpty(info.getTextbookName())) {
					return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "textbookName不能为空");
				}if (StringUtils.isEmpty(info.getChapterName())) {
					return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "chapterName不能为空");
				}if (StringUtils.isEmpty(info.getUserId())) {
					return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "userId不能为空");
				}if (StringUtils.isEmpty(info.getLoginPlatformCode())) {
					return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "loginPlatformCode不能为空");
				}

				CatalogInfo ci = wxCloudService.getCatalogByUser(info);
				if(ci == null) {
                    wxCloudService.saveCatalogUser(info);
                } else {
				    wxCloudService.updateCatalogUser(info);
                }

				return ResponseEntity.getSuccessResponse();
			}
		} catch (Exception e) {
            logger.error(e.getMessage());
			return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "保存用户上次选择的教材章节出错");
		}
    }

    /**
	 *
	 * @Desc 获取用户上次选择的教材章节
	 * @param CatalogInfo  编目信息 实体类
	 */
	@RequestMapping(value="/getCatalogByUser",method=RequestMethod.POST)
	public ResponseEntity getCatalogByUser(HttpServletRequest request, HttpServletResponse response,
                                                @RequestBody CatalogInfo info) {
		try {
			if (null == info) {
				return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "请求参数不能为空");
			}
			if (StringUtils.isEmpty(info.getUserId())) {
				return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "userId不能为空");
			}
			if (StringUtils.isEmpty(info.getLoginPlatformCode())) {
				return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "loginPlatformCode不能为空");
			}
			CatalogInfo result = wxCloudService.getCatalogByUser(info);

			return ResponseEntity.getSuccessResponse(result);
		} catch (Exception e) {
		    logger.error(e.getMessage());
			return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "取用户上次选择的教材章节出错");
		}
	}
}
